# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=aalib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4rc5
_realver=1.4.0
pkgrel=2
pkgdesc="A portable ASCII art graphic library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://aa-project.sourceforge.net/aalib/"
license=('LGPL')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://downloads.sourceforge.net/sourceforge/aa-project/${_realname}-${pkgver}.tar.gz
        0001-Apply-patch-for-MSYS2.patch
        0002-configure-src-tweak-Windows-link-flags.patch
        0003-aalib-m4-quote-macro-name.patch
        0004-aalib-m4-use-correct-variable.patch)
sha256sums=('fbddda9230cf6ee2a4f5706b4b11e2190ae45f5eda1f0409dc4f99b35e0a70ee'
            '5a243ac641912749c00384472b0d7b904c4a1052273e4e19b6be747ed77fb9ff'
            'baec2b331d4adf1c94489f658974993163ab327130e49a1f5eefe22743970c5f'
            '3e719aac18c67ea19655498beaab814131d3b408a77cbdaaeed86c8a333d3f9d'
            '90515395eafe6ca6c7d8ac8226fd4e0976dc1d6dfebf8d1f0a7bdde3807eca5e')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${_realver}

  # From https://gitlab.gnome.org/GNOME/gimp/-/tree/master/build/windows/patches
  # https://gitlab.archlinux.org/archlinux/packaging/packages/aalib
  _apply_patch_with_msg \
    0001-Apply-patch-for-MSYS2.patch \
    0002-configure-src-tweak-Windows-link-flags.patch \
    0003-aalib-m4-quote-macro-name.patch \
    0004-aalib-m4-use-correct-variable.patch

  WANT_AUTOCONF=2.69 autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../"${_realname}-${_realver}"/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${_realver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
